define(function (require, exports, module) {
    var template = require("template");
    var Ajax = require("ajax");
    var layer = require("layer");
    var util = require('util');
    
    var passObj = function (callback) {
        this.init.apply(this,arguments);
    };
    passObj.prototype = {
        constructor: passObj,
        options: {},
        init: function (options) {
            var t = this;
            t.options = $.extend({},t.options,options);
            $(".tipConNav").text("已发报送");
            $(window).off('scroll');
            $("#rightConBox").html(template("submitPassTemp",{}));
            t.mask();
            t.loadInitList(1,"submit/queryPost.do");
            t.bindNav();
        },
        
		bindNav : function() {
			var t = this;
			if ($.cookie("roleEnum") == "user_com") {
				//submit,submit_pass,submit_audit
				$(".topNav #submit").remove();
				$(".topNav #submitPass").remove();
			}else if($.cookie("roleEnum") == "user_simple"){
				$(".topNav #submitAudit").remove();
			}else if($.cookie("roleEnum")=="admin_com"){
				$(".topNav #submit").remove();
				$(".topNav #submitAudit").remove();
			}
		},
        
        mask: function(){
            $(".mask").css("height",$(document).height());
            $(".mask").css("width",$(document).width());
        },
        
        loadInitList: function(pageIndex,url) {
			var t = this;
//			$(".respond").css('display','none');
			Ajax.ajax({
				url: localhost + url,
				data: {
					pageIndex: pageIndex
				},
				beforeSend: function() {
					if(pageIndex==1){
						$('#pass_table>tbody').html(template("loadingDom"));
					}else{
						$("#listDom").append("<div class='loadingBox animated fadeIn nextloding'><span>正在加载中，请稍后</span></div>");
					}
				},
				success: function(data) {
    				//防止失效的请求影响到下次页面的滚动加载
    				var href="";
    				if($.cookie("roleEnum") == "admin_com"){
    					href = window.location.href.indexOf("admin/submit");
    				}else{
    					href = window.location.href.indexOf("admin/submit_pass");
    				}
    				if(href < 0 ) {
    					return;
    				}
					if(data.returnCode == 10000){
						if(data.returnData && data.returnData.length > 0) {
							$(".loadingBox").remove();
							var tHead='',tList='',submitState='',submitLevel='',colors='',
								dataList = data.returnData;
							
							tHead += "<tr class='th'><th>发起时间</th><th>标题</th>";
//							if($.cookie("roleEnum")=="admin_com"){
								tHead += "<th>报送人</th>";
//							}
							tHead += "<th>报送对象</th><th>报送状态</th><th>批复</th><th>操作</th></tr>";
			                for (var i = 0; i < dataList.length; i++) {
			                    var item = dataList[i]; 
			                    //表格
			                    tList += '<tr><td>'+item.postTime+'</td>';
			                    tList += '<td>'+item.submitTitle+'</td>';
//								if($.cookie("roleEnum")=="admin_com"){
									tList += '<td>'+ item.postUserName +'</td>';
//								}
			                    tList += '<td>'+item.receiveUserName+'</td>';
			                    if(item.submitState==0){
			                    	submitState = '未审核';
			                    	colors = "yellow";
			                    }else if(item.submitState==1){
			                    	submitState = '已审阅';
			                    	colors = "green";
			                    }else if(item.submitState==2){
			                    	submitState = '被驳回';
			                    	colors = "red";
			                    }
			                    tList += '<td class="'+ colors +'">'+submitState+'</td>';
				                tList += '<td>'+item.submitReply+'</td>';
				                
								tList += '<td><a href="javascript:;" class="details" ';
								tList += isNull(item.submitTitle,'data-submitTitle=');
								tList += isNull(item.source,'data-source=');
								tList += isNull(item.informationName,'data-informationName=');
								tList += isNull(item.receiveDepartName,'data-receiveDepartName=');
								tList += isNull(item.receiveUserName,'data-receiveUserName=');
								tList += isNull(item.submitLevel,'data-submitLevel=');
								tList += isNull(item.submitContent,'data-submitContent=');
								tList += isNull(item.submitReply,'data-submitReply=');
								tList += isNull(item.submitState,'data-submitState=');
								
								tList += '>详情</a></td></tr>';
			                }
			                
							//如果属性值为空，那么此元素就不添加该属性
							function isNull(values,keys){
								if(values||values=='0'){
									var tL = keys + "'"+values+"'";
								}else{
									tL = '';
								}
								return tL;
							}
			                
			                if(pageIndex==1){
			                	$('#pass_table>tbody').html('');
			                	$('#pass_table>tbody').append(tHead+tList);
			                }else{
			                	$('#pass_table>tbody').append(tList);
			                }
//			                util.CutOff();      //截断过长文字
			                if(href > -1 ) {
			                	t.loadPage(pageIndex,'submit/queryPost.do');
			                }
		
						} else {
							$(".loadingBox").remove();
							$(window).off('scroll');
							if(pageIndex==1){
								$('#pass_table>tbody').html(template("noDataDom"));
							}
						}
						t.bindDetailsEvent();
					}
				}
			});
		},
		loadPage: function(pageIndex,url,timeRange) {
			var t = this;
			//获取页面顶部被卷起来的高度
			function scrollTop() {
				return Math.max(
					//chrome
					document.body.scrollTop,
					//firefox/IE
					document.documentElement.scrollTop);
			}
			//获取页面文档的总高度
			function documentHeight() {
				//现代浏览器（IE9+和其他浏览器）和IE8的document.body.scrollHeight和document.documentElement.scrollHeight都可以
				return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
			}
			function windowHeight() {
				return(document.compatMode == "CSS1Compat") ?
					document.documentElement.clientHeight :
					document.body.clientHeight;
			}
			var href="";
			if($.cookie("roleEnum") == "admin_com"){
				href = window.location.href.indexOf("admin/submit");
			}else{
				href = window.location.href.indexOf("admin/submit_pass");
			}
			if(href>-1){
			}else{
				return false;
			}
			var isloading = false;
			$(window).off('scroll');
			$(window).on('scroll', function() {
				//判定是否加载中，防止重复加载
				if(!isloading){
					//滚动条卷起来的高度 + 窗口高度 >= 文档的总高度 - 50/*这里将滚动响应区域高度取50px*/；如果这个判断为true则表示滚动条滚动到了底部。
					if(scrollTop() + windowHeight() >= (documentHeight() - 50)) {
						isloading = false;
						pageIndex = parseInt(pageIndex)+ 1;
						if(href>-1){
						}else{
							return false;
						}
						t.loadInitList(pageIndex,url);
						isloading = true;
					}
				}
			});
		},
		
		bindDetailsEvent: function(){
			var t = this;
			$('.isRejectBtn').css('display','none');
			$('.isAuditBtn').css('display','none');
			// 已发报送详情查看
			$(".details").unbind("click").on("click",function() {
				$(".body_submit_pass").css('display','none');
				$("#twobox").css('display','block');
				$("#twobox").html('');
			
				var title_value = $(this).attr("data-submitTitle");
				var source_value = $(this).attr("data-source");
				var types = $(this).attr("data-informationName");
				var departments = $(this).attr("data-receiveDepartName");
				var leaders = $(this).attr("data-receiveUserName");
				
				var submitLevel = $(this).attr("data-submitLevel");
				if(submitLevel==1){
					var grades = '★';
				}else if(submitLevel==2){
					var grades = '★★';
				}else if(submitLevel==3){
					var grades = '★★★';
				}else if(submitLevel==4){
					var grades = '★★★★';
				}else if(submitLevel==5){
					var grades = '★★★★★';
				}
				
				var content_content_text = $(this).attr("data-submitContent");
				var content_content_respond = $(this).attr("data-submitReply");
				
				$("#twobox").html(template("submitDetails"));
				if($(this).attr("data-submitState")=="0"){
					$(".respond").css('display','none');
				}else{
					$(".respond").css('display','block');
				}
				$(".isRejectBtn").remove();
				$(".isAuditBtn").remove();
				$("#title_value").val(title_value).attr('readonly','readonly').addClass('color_hui');
				$("#source_value").val(source_value).attr('readonly','readonly').addClass('color_hui');
				$("#types").val(types).attr('readonly','readonly').addClass('color_hui');
				$("#departments").val(departments).attr('readonly','readonly').addClass('color_hui');
				$("#leaders").val(leaders).attr('readonly','readonly').addClass('color_hui');
				$("#grades").val(grades).attr('readonly','readonly').addClass('color_hui');
				$("#content_content_text").val(content_content_text).attr('readonly','readonly').addClass('color_hui');
				$("#content_content_respond").val(content_content_respond).attr('readonly','readonly').addClass('color_hui');
				t.controlDetailsForm();
			});
		},
		
		controlDetailsForm: function(){
			var t= this;
			//返回
			$("#content_cancel").unbind('click').on('click',function(){
				$(".body_submit_pass").css('display','block');
				$("#twobox").css('display','none');
			});
		}
    };
    module.exports = passObj;
});

